﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ActivityWindow.cs" company="YellowFish">
//   Copyright © YellowFish 2009
// </copyright>
// <summary>
//   Describes and manipulates the activity window.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MobilePal.Dialogs
{
  #region Imported Namespaces


  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Linq;
  using System.Windows.Forms;

  using State;


  #endregion


  /// <summary>
  /// Describes and manipulates the activity window.
  /// </summary>
  public partial class EditActivityDialog: Form
  {
    #region Constants and Fields


    private readonly Activity _activity;
    private readonly ActivityCategoryCollection _categories;
    private ActivityCategoryCollection _activityCategories;


    #endregion


    #region Constructors and Destructors


    /// <summary>
    /// Initializes a new instance of the <see cref="EditActivityDialog"/> class.
    /// </summary>
    public EditActivityDialog(Activity activity, ActivityCategoryCollection categories)
    {
      InitializeComponent();

      _categories = categories;

      if (activity != null)
      {
        _activity = activity;
        _activityCategories = new ActivityCategoryCollection(_activity.Categories);
      }
      else
      {
        _activity = new Activity();
        _activityCategories = new ActivityCategoryCollection(new List<ActivityCategory> {categories.DefaultCategory});
      }

      activityName.Text = _activity.Name;

      BuildCategoriesList();
    }


    #endregion


    #region Properties


    /// <summary>
    /// Gets the activity.
    /// </summary>
    /// <value>
    /// The activity.
    /// </value>
    public Activity Activity
    {
      get
      {
        return _activity;
      }
    }


    #endregion


    #region Methods


    private void BuildCategoriesList()
    {
      categoriesList.Items.Clear();

      var categories = new List<ActivityCategory>(_activityCategories);
      categories.Sort((category1, category2) => string.Compare(category1.Name, category2.Name));

      foreach (ActivityCategory category in categories)
      {
        categoriesList.Items.Add(category.Name);
      }
    }


    private void CancelMenuItemClick(object sender, EventArgs e)
    {
      DialogResult = DialogResult.Cancel;
      Close();
    }


    /// <summary>
    /// Raises the <see cref="E:System.Windows.Forms.Form.Closing"/> event.
    /// </summary>
    /// <param name="e">A <see cref="T:System.ComponentModel.CancelEventArgs"/> that contains the event data.</param>
    protected override void OnClosing(CancelEventArgs e)
    {
      if(DialogResult != DialogResult.OK)
      {
        return;
      }

      if (string.IsNullOrEmpty(activityName.Text))
      {
        MessageBox.Show("Invalid activity name.", "Invalid Name", MessageBoxButtons.OK, MessageBoxIcon.Hand,
                        MessageBoxDefaultButton.Button1);

        e.Cancel = true;
      }

      if(e.Cancel)
      {
        return;
      }

      _activity.Name = activityName.Text;

      foreach (ActivityCategory category in _activityCategories)
      {
        if (!_activity.Categories.Contains(category))
        {
          _activity.Categories.Add(category);
        }
      }
    }


    private void EditCategoriesMenuItemClick(object sender, EventArgs e)
    {
      var dialog = new CategoriesListDialog(_categories);
      dialog.ShowCheckBoxes = true;
      dialog.SelectedItems = new List<object>(_activityCategories.Cast<object>());
      DialogResult result = dialog.ShowDialog();

      if (result != DialogResult.OK)
      {
        return;
      }

      _activityCategories =
        new ActivityCategoryCollection(new List<ActivityCategory>(dialog.SelectedItems.Cast<ActivityCategory>()));
      BuildCategoriesList();
    }


    private void OkMenuItemClick(object sender, EventArgs e)
    {
      DialogResult = DialogResult.OK;
      Close();
    }


    #endregion
  }
}